//
//  MainSeachBrandVC.m
//  ZITHER
//
//  Created by weixin on 2018/7/25.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainSeachBrandVC.h"
#import "MainSeachBrandCell.h"
#import "MainSeachBCell.h"
#import "UITableView+SCIndexView.h"

@interface MainSeachBrandVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) BOOL translucent;
@property (nonatomic,strong)NSMutableDictionary * dicModel;
@property (nonatomic,strong)NSMutableArray * arrM;
@property (nonatomic,strong)NSMutableArray * arrMBigWord;//首字母

@end

@implementation MainSeachBrandVC

#pragma mark - Life Cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNav];
    [self loadDate];
}




-(void)setNav{
    [self.baseNavigationView setHiddenNav:YES VC:self];

    //登录或注册
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.shadowImage = [UIImage new];
//    [self.baseNavigationView setBackgroundColor:[UIColor clearColor]];
//    self.baseNavigationView.bottomLineView.backgroundColor = [UIColor clearColor];
//    self.baseNavigationView.leftButton1.hidden = YES;
//    [UIColor setDefaulGradiennBackgroundColors:self.view];
    /* TODO: ================= 导航栏箭头颜色 =================*/
    
    [self.baseNavigationView NavigationBarStyle:1];
    self.baseNavigationView.navViewStyle.title.text = @"";
    self.baseNavigationView.navViewStyle.leftBtn.hidden = YES;
    self.baseNavigationView.leftButton1.hidden = YES;
    self.baseNavigationView.bottomLineView.hidden = YES;
    
    BaseLab *lab = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 0, 1) text:@"搜索条件   >   品牌" font:1 textColor:[UIColor colorWithHex:@"333333"] isBold:NO numberOfLines:0];
    lab.font = [UIFont fontWithName:@"PingFangSC-Medium" size:17];
    [self.view addSubview:lab];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(TabbarHeight-5);
        make.left.equalTo(self.view).offset(15);
    }];
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab.mas_bottom).offset(0);
        make.left.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(-50);
    }];
    
}

-(void)setUI{
    
//    QMUILabel *leb = [[QMUILabel alloc]initWithFrame:CGRectMake(20,50, 200, 40)];
//    leb.text = @"关键词寻表";
//    leb.font = [UIFont systemFontOfSize:18];
//    leb.textColor = [UIColor whiteColor];
//    [self.view addSubview:leb];
   
    self.translucent = YES;
    
  
    QMUIButton *btn = [[QMUIButton alloc]initWithFrame:CGRectMake(0,kScreenH-50,kScreenW,50)];
    [btn setTitle:@"取消" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithHex:@"333333"] forState:UIControlStateNormal];
    
    btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15];

//    [btn setImage:[UIImage imageNamed:@"key_btn_back_white"] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    @weakify(self);
    [[btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
}



-(void)loadDate{
    
        [[serviceDefualt loadGoodsSearchConditions:[searchConditionsModel class]]subscribeNext:^(ResponseData * x) {
            self.model = x.responseData;
            
            NSDictionary *dic =[self.model.brandInfo yy_modelToJSONObject];
            
            self.dicModel = [[NSMutableDictionary alloc]initWithDictionary:dic];
            
            NSMutableArray *indexViewDataSource = [NSMutableArray array];
            
            NSArray *newArray = [[self.dicModel allKeys] sortedArrayUsingSelector:@selector(compare:)];
            

            for (NSString *str in newArray) {
                //填充字母
                [indexViewDataSource addObject:str];
                [self.arrM addObject: self.dicModel[str]];
                [self.arrMBigWord addObject:str];
            }
            
            [self setUI];

            if (self.hasSearch) {
                [indexViewDataSource insertObject:UITableViewIndexSearch atIndex:0];
            }
            
            self.tableView.sc_indexViewDataSource = indexViewDataSource.copy;
            [self.tableView reloadData];
   
        }];
    
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"indexPath == %@",indexPath);
    NSLog(@"");
    if (self.block) {
        self.block(self.arrM[indexPath.section][indexPath.row]);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arrM.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = self.arrM[section];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    static NSString *sectionCell = @"MainSeachBrandCell";
    MainSeachBrandCell *cell = [tableView dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[MainSeachBrandCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    
    NSString *brandName = self.arrM[indexPath.section][indexPath.row][@"brandNameNative"];
    if ([self.modelSelect.brandName isEqualToString:brandName]) {
        cell.title.textColor = [UIColor whiteColor];
        cell.subtitle.textColor = [UIColor whiteColor];
        cell.view.backgroundColor = [UIColor blackColor];
    }else{
        cell.title.textColor = [UIColor blackColor];
        cell.subtitle.textColor = [UIColor blackColor];
        cell.view.backgroundColor = [UIColor whiteColor];
    }
    
    cell.title.text = brandName;

    cell.subtitle.text =self.arrM[indexPath.section][indexPath.row][@"brandName"];

    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 55)];
    view.backgroundColor = [UIColor clearColor];
    QMUILabel *lab  =[[QMUILabel alloc]initWithFrame:CGRectMake(35, 0, SCREEN_WIDTH-40, 30)];
//    NSArray *arr = [self.dicModel allKeys];
    lab.text =[NSString stringWithFormat:@"拼音%@",self.arrMBigWord[section]];
    lab.font = [UIFont systemFontOfSize:25];
    lab.centerY = view.centerY;
    lab.textColor = [UIColor colorWithHex:@"333333"];
    [view addSubview:lab];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 55;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat heightCell = 0;
    /* TODO: ================= cell 高度通过类型区分 =================*/
    
    heightCell = 100;
    
    return heightCell;
}

//设置区头不悬浮
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 50;
    
    if(scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0,0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }
    
}

#pragma mark - Getter and Setter

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 90, kScreenW, kScreenH-90-100) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerNib:[UINib nibWithNibName:@"MainSeachBrandCell" bundle:nil] forCellReuseIdentifier:@"MainSeachBrandCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"MainSeachBCell" bundle:nil] forCellReuseIdentifier:@"MainSeachBCell"];

        
        if (self.hasSearch) {
            
            UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 100)];
            QMUIButton *btn = [[QMUIButton alloc]initWithFrame:CGRectMake(20, 5, kScreenW-100, 40)];
            [btn setTitle:@"不限" forState:UIControlStateNormal];
            @weakify(self);
            [[btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
                @strongify(self);
                /* TODO: ================= 未知点击 =================*/
            }];
            [btn setBorderWithColor:kMainColorGray width:1];
            [btn setTitleColor:kMainColorGray forState:UIControlStateNormal];
            [btn setCornerRadiusWithRadius:8 isMasks:YES];
            btn.centerY = view1.centerY;
            [view1 addSubview:btn];
            
            self.tableView.tableHeaderView = view1;
            
        }
        
        SCIndexViewConfiguration *configuration = [SCIndexViewConfiguration configurationWithIndexViewStyle:self.indexViewStyle];
        [self setConfiguration:configuration];
        _tableView.sc_indexViewConfiguration = configuration;
        _tableView.sc_translucentForTableViewInNavigationBar = self.translucent;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        
       
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 55)];
        view.backgroundColor = [UIColor clearColor];
        QMUILabel *lab  =[[QMUILabel alloc]initWithFrame:CGRectMake(18, 0, SCREEN_WIDTH-65, 40)];
        lab.text = @"不限";
        lab.font = [UIFont systemFontOfSize:18];
        lab.textAlignment = NSTextAlignmentCenter;
        lab.centerY = view.centerY;
        lab.textColor = [UIColor colorWithHex:@"333333"];
        [lab setCornerRadiusWithRadius:8 isMasks:YES];
        [lab setBorderWithColor:[UIColor colorWithHex:@"333333"] width:1.5];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(id x) {
            if (self.blockType) {
                self.blockType();
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
        [view addGestureRecognizer:tap];
        [view addSubview:lab];
        _tableView.tableHeaderView = view ;
    }
    return _tableView;
}

-(void)setConfiguration:(SCIndexViewConfiguration*)configuration{
    
    configuration.indicatorBackgroundColor = baseColorTheme;
    configuration.indicatorHeight = 50;
    configuration.indicatorRightMargin = 50;
    configuration.indicatorCornerRadius = 20;
    configuration.indexItemBackgroundColor = [UIColor clearColor];
    configuration.indexItemSelectedBackgroundColor = [UIColor whiteColor];
    configuration.indexItemHeight = 20;
    configuration.indexItemRightMargin = 20;
    configuration.indexItemsSpace = 7;

    configuration.indexItemTextColor = [UIColor colorWithHex:@"333333"];
    configuration.indicatorTextColor = [UIColor whiteColor];//选中文字颜色
    configuration.indexItemSelectedTextColor = baseColorTheme;//右边选中文字颜色
    configuration.indicatorHeight = 60;

    //    @property (nonatomic, assign) CGFloat indexItemHeight;                      // 索引元素高度
 
}

-(NSMutableDictionary *)dicModel{
    if (!_dicModel) {
        _dicModel = [[NSMutableDictionary alloc]init];
    }
    return _dicModel;
}

-(NSMutableArray *)arrM{
    if (!_arrM) {
        _arrM = [[NSMutableArray alloc]init];
    }
    return _arrM;
}

-(NSMutableArray *)arrMBigWord{
    if (!_arrMBigWord) {
        _arrMBigWord = [[NSMutableArray alloc]init];
    }
    return _arrMBigWord;
}


@end
